# -*- coding: utf-8 -*-
__author__ = 'chen'

import numpy as np
from sklearn import tree
from sklearn import preprocessing
from sklearn.feature_extraction import DictVectorizer

class DicTree:
    def __init__ (self):
        self.feature_list=[]
        data = np.loadtxt("./Lianxi1_8/tree.txt",dlimiter="\t",dtype=np.str)

        x_data=data[1:,:-1]
        y_data=data[1:,-1:]
        title = data[:1,:]
        self.vec = DictVectorizer()
        for row in x_data:
            DictRow={};
            for f in range(0,len(row)):
                DictRow[title[f]]=row[f]
        self.feature_list.append(DictRow)

        def transData(self):
            X = self.vec.fit_transform(self.feature_list).toarray()
            lb = preprocessing.LabelBinarizer()
            y = lb.fit_transform(self.y_data)
            return X, y

        def dicsionTree(self):
            model = tree.DecisionTreeClassifier(criterion='entropy')
            X, y = d.transData()
            model.fit(X, y)
            p = model.predict(X)
            print(p)

    d = DicTree()
    d.dicsionTree()
